import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: '/page',
    name: 'page',
    component: () => import('../views/page/index.vue'),
    children: [
      {
        path: '/page/',
        name: 'houtai',
        component: () => import('../views/page/houtai.vue')
      },
      {
        path: '/page/xiangce',
        name: 'xiangce',
        component: () => import('../views/page/xiangce.vue')
      },
      {
        path: '/page/shangpin',
        name: 'shangpin',
        component: () => import('../views/page/shangpin.vue')
      },
    ]
  },
  {
    path: '/commodity',
    name: 'commodity',
    component: () => import('../views/commodity/index.vue'),
    children: [
      {
        path: '/commodity/',
        name: 'Productlist',
        component: () => import('../views/commodity/Productlist.vue')
      },
      {
        path: '/commodity/detail',
        name: 'detail',
        component: () => import('../views/commodity/detail.vue')
      },
      {
        path: '/commodity/viewproduct',
        name: 'Productlist',
        component: () => import('../views/commodity/viewproduct.vue')
      },
    ]
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/order/index.vue'),
    children: [
      {
        path: '/order/',
        name: 'orderjurisdiction',
        component: () => import('../views/order/orderjurisdiction.vue')
      },
      {
        path: '/order/vieworder',
        name: 'vieworder',
        component: () => import('../views/order/vieworder.vue')
      },
    ]
  },
  {
    path: '/vip',
    name: 'vip',
    component: () => import('../views/vip/index.vue'),
    children: [
      {
        path: '/vip/',
        name: 'vipjurisdiction',
        component: () => import('../views/vip/vipjurisdiction.vue')
      },
      {
        path: '/vip/vipPreemption',
        name: 'vipPreemption',
        component: () => import('../views/vip/vipPreemption.vue')
      },
    ]
  },
  {
    path: '/setup',
    name: 'setup',
    component: () => import('../views/setup/index.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }

]

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
})
router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token")) {
    next()
  } else {
    if (to.path == "/login") {
      next()
    } else {
      next("/login")
    }
  }
})
export default router
